package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Resource;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.ResourceService;
import com.itwn.comic.vo.ResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("resource")
public class ResourceController {
    @Value("${imgPath}")
    String imgPath;
    @Autowired
    ResourceService resourceService;

    // 唯一的图片处理的方法
    @RequestMapping("/upload")
    public ResponseResult<String> upload(@RequestParam(value ="headerImg") MultipartFile uploadFile,Integer upId){
        System.out.println(uploadFile.getOriginalFilename());
        String filename = uploadFile.getOriginalFilename();
        // 重新获取名字，保证不重复
        System.out.println(upId+"==========");
        String subFix = filename.substring(filename.lastIndexOf("."));
        filename = "comic" + System.currentTimeMillis()+subFix;
        // E:/imgs/resource/id数字/comic2858292783742947.jpg
        File file = new File(imgPath + "resource/"+upId+"/"+filename);

        System.out.println(imgPath + "resource/"+upId+"/"+filename);
        if(!file.exists()){
            file.mkdirs();
            System.out.println(file.mkdirs());
        }

        ResponseResult<String> result = new ResponseResult<>();
        try {
            uploadFile.transferTo(file);
            result.setCode(200);
            result.setMsg("上传成功");
            result.setData(filename);
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("上传失败");
        }
        return result;
    }









    @GetMapping("/list")
    public ResponseResult<PageInfo<ResourceVo>> list(@RequestParam(value = "size",defaultValue = "5")Integer size,
                                                     @RequestParam(value = "now",defaultValue = "1")Integer now,
                                                     String name){
        ResponseResult<PageInfo<ResourceVo>> result = new ResponseResult<>();
        try {
            PageInfo<ResourceVo> pageInfo = resourceService.queryResourceByPage(size,now,name);
            if(pageInfo != null){
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }else {
                result.setCode(201);
                result.setMsg("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }

    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Resource resource){
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            int i = resourceService.addResource(resource);
            if(i != 0){
                result.setCode(200);
                result.setMsg("添加成功");
            }else {
                result.setCode(201);
                result.setMsg("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return  result;
    }

    @PutMapping("/modify")
    public ResponseResult<Void> modify(@RequestBody Resource resource){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            int i = resourceService.modifyRes(resource);
            if(i != 0){
                result.setCode(200);
                result.setMsg("修改成功");
            }else {
                result.setCode(201);
                result.setMsg("修改失败");
            }
        }catch (Exception e){
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }
    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> del(@PathVariable("id")int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            resourceService.removeRes(id);
                result.setCode(200);
                result.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败");
        }
        return  result;
    }

    @RequestMapping("/lock")
    public ResponseResult<Void> lock(@RequestParam(value = "id") Integer id,@RequestParam(value = "status") Integer status){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            int i = resourceService.lockResource(id,status);
            if(i != 0){
                result.setCode(200);
                result.setMsg("修改状态成功");
            }else {
                result.setCode(201);
                result.setMsg("修改状态失败");
            }
        }catch (Exception e){
            result.setCode(201);
            result.setMsg("修改状态失败");
        }
        return result;
    }
}
